package com.proyecto.track.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.opensymphony.xwork2.ActionSupport;
import com.proyecto.track.bo.Project;
import com.proyecto.track.bo.Task;
import com.proyecto.track.bo.TaskStatus;
import com.proyecto.track.bo.TaskType;
import com.proyecto.track.service.TaskService;
import com.proyecto.track.service.UserPersonService;

public class TaskAction extends ActionSupport{
	private static final long serialVersionUID = 1L;

	private Task task;
	private Long id;
	private Long idProject;
	private TaskService taskService;
	@Autowired
	private UserPersonService userPersonService;
	private TaskType taskType;
	private TaskStatus taskStatus2;
	private List<TaskStatus> taskStatus = new ArrayList<TaskStatus>();
	private List<TaskType> taskTypes = new ArrayList<TaskType>();
	private List<Task> tasks = new ArrayList<Task>();
	private Date startDate;
	private Date finishDate;
	private Project project = new Project();
	private String msg;
	private Long[] newUsersInsideTask; 
	private Long[] newUsersOutsideTask;
	

	private void loadLists(){
		this.taskStatus = taskService.listTaskStatus();
		this.taskTypes = taskService.listTaskType();
	}
	
	public String newTask(){
		this.loadLists();
		return SUCCESS;
	}
	
	public String saveTask(){
		this.loadLists();
		if(task.getId() == null || task.getId()== 0){
			project = taskService.createTask(task,idProject);
			msg = "The task was created successfully";
		}else{
			taskService.modifyTask(task);
		}
		this.task = null;
		this.startDate = null;
		this.finishDate = null;
		return SUCCESS;
	}
	
	public String listTasks(){
		this.tasks = taskService.searchTask();
		return SUCCESS;
	}
	
	public String searchTask(){
		this.task = taskService.searchTask(this.id);
		return SUCCESS;
	}
	
	public String modifyTask(){
		this.loadLists();
		this.searchTask();
		this.startDate = this.task.getPeriod().getStartDate();
		this.finishDate = this.task.getPeriod().getEndDate();
		return SUCCESS;
	}
	
	public String saveTypeTask(){
		taskService.saveTypeTask(this.taskType);
		this.taskType = null;
		return SUCCESS;
	}

	public String saveStatusTask(){
		taskService.saveStatusTask(this.taskStatus2);
		this.taskStatus2 = null;
		return SUCCESS;
	}
	
	public String saveUsersTask(){
		taskService.modify(this.id, userPersonService.searchUsersPerson(newUsersInsideTask));
		return "SUCCESS";
	}
	
	public Task getTask() {
		return task;
	}
	public void setTask(Task task) {
		this.task = task;
	}
	
	public Long getIdProject() {
		return idProject;
	}
	public void setIdProject(Long idProject) {
		this.idProject = idProject;
	}
	
	public TaskService getTaskService() {
		return taskService;
	}
	public void setTaskService(TaskService taskService) {
		this.taskService = taskService;
	}
	
	public List<TaskStatus> getTaskStatus() {
		return taskStatus;
	}
	public void setTaskStatus(List<TaskStatus> taskStatus) {
		this.taskStatus = taskStatus;
	}
	
	public List<TaskType> getTaskTypes() {
		return taskTypes;
	}
	public void setTaskTypes(List<TaskType> taskTypes) {
		this.taskTypes = taskTypes;
	}

	public List<Task> getTasks() {
		return tasks;
	}

	public void setTasks(List<Task> tasks) {
		this.tasks = tasks;
	}

	public Date getStartDate() {
		return startDate;
	}

	public void setStartDate(Date startDate) {
		this.startDate = startDate;
	}

	public Date getFinishDate() {
		return finishDate;
	}

	public void setFinishDate(Date finishDate) {
		this.finishDate = finishDate;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public TaskType getTaskType() {
		return taskType;
	}

	public void setTaskType(TaskType taskType) {
		this.taskType = taskType;
	}

	public TaskStatus getTaskStatus2() {
		return taskStatus2;
	}

	public void setTaskStatus2(TaskStatus taskStatus2) {
		this.taskStatus2 = taskStatus2;
	}
	
	public Project getProject() {
		return project;
	}

	public void setProject(Project project) {
		this.project = project;
	}
	
	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public Long[] getNewUsersInsideTask() {
		return newUsersInsideTask;
	}

	public void setNewUsersInsideTask(Long[] newUsersInsideTask) {
		this.newUsersInsideTask = newUsersInsideTask;
	}

	public Long[] getNewUsersOutsideTask() {
		return newUsersOutsideTask;
	}

	public void setNewUsersOutsideTask(Long[] newUsersOutsideTask) {
		this.newUsersOutsideTask = newUsersOutsideTask;
	}
	
	
}
